package com.woniu.mall.service.impl;

import com.woniu.mall.constant.BusinessRedisKey;
import com.woniu.mall.pojo.Coupon;
import com.woniu.mall.service.CouponBusinessService;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Key;

/**
 * 1. @description:
 * 2. @author: Lingger
 * 3. @time: 2023/5/23
 */

@Service
public class CouponBusinessServiceImpl implements CouponBusinessService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Boolean saveBusinessCoupon(UserLoginVO userLoginVO) {

        //如果该用户不存在商家id 说明没有注册商户 不允许发布商家优惠券
        Boolean flag = isStore(userLoginVO);
        Coupon coupon = createBusinessCoupon();
        redisTemplate.boundHashOps(BusinessRedisKey.BUSINESS_COUPON).put(userLoginVO.getStoreId().toString(), coupon);
        return flag;
    }

    @Override
    public Boolean removeBusinessCoupon(UserLoginVO userLoginVO) {

        //如果该用户不存在商家id 说明没有注册商户 不允许删除商家优惠券
        Boolean flag = isStore(userLoginVO);

        redisTemplate.boundHashOps(BusinessRedisKey.BUSINESS_COUPON).delete(userLoginVO.getStoreId().toString());
        return flag;
    }

    @Override
    public Coupon createBusinessCoupon() {
        Coupon coupon = new Coupon();
        coupon.setName(BusinessRedisKey.BUSINESS_COUPON);
        coupon.setNumber(BusinessRedisKey.NUMBER);
        coupon.setAmount(BusinessRedisKey.AMOUNT_4);
        return coupon;
    }

    //判断是否为商家
    private Boolean isStore(UserLoginVO userLoginVO) {
        if (userLoginVO.getStoreId() == null) {
            return false;
        }
        return true;
    }
}
